<?php
	class TradeModel extends Model{
		public $bplist;		//竞赛项目列表
		public $page;		//分页的页号(当前页)
		public $evelpage=8;	//每页显示两条记录。
		public $pagenum;	//总共的页数。
		public $enterprise;	//企业列表
		public $like;		//搜索关键字
		public $org;		//搜索企业
		
		
		public function __construct($page,$like="",$org){
			$this->page = $page;
			$this->like = $like;
			$this->org = $org;
			$this->enterprise = $this->getenterprise();
			$this->pagelist();
		}
		
		public function getbproblemlist(){
			if($this->org==0){
				$sql='	SELECT b.Id, b.Title,b.Description,b.EnterpriseId,e.Name,b.UpdateTiem,e.WebUrl,b.ViewCount,b.ChoiceCount
						FROM businessproblems b,enterprise e
						WHERE b.EnterpriseId=e.Id and e.IsPublic=1 and b.Title like "%'.$this->like.'%" ORDER BY b.CreateDate ';
			}elseif($this->org==-1){
				$sql='	SELECT b.Id, b.Title,b.Description,b.EnterpriseId,e.Name,b.UpdateTiem,e.WebUrl,b.ViewCount,b.ChoiceCount
						FROM businessproblems b,enterprise e
						WHERE b.EnterpriseId=e.Id and e.IsPublic=0 and b.Title like "%'.$this->like.'%" ORDER BY b.CreateDate ';
			}else{
				$sql='	SELECT b.Id,b.Title,b.Description,b.EnterpriseId,e.Name,b.UpdateTiem,e.WebUrl,b.ViewCount,b.ChoiceCount
						FROM businessproblems b,enterprise e
						WHERE b.EnterpriseId=e.Id and e.Id='.$this->org.' and b.Title like "%'.$this->like.'%" ORDER BY b.CreateDate ';
			}
		
		
			$list=M()->query($sql);
			return $list;
		}
		
		public function pagelist(){
			$page = $this->page;		//当前页号
			$pinfo = $this->getbproblemlist();
			$count=count($pinfo);		//总记录数
			$page=($page<1)?1:(($page>ceil($count/$this->evelpage))?ceil($count/$this->evelpage):$page);
			$t=(($page-1)*($this->evelpage));
			for($i=0;$i<($this->evelpage);$i++){
				if($pinfo[$t+$i])
					$log[$i]=$pinfo[$t+$i];
			}
			$this->bplist = $log;
			$this->pagenum = ceil($count/$this->evelpage);
		}
		
		public function getenterprise(){
			return $list=M()->query('SELECT Id,Name FROM enterprise');
		}
	}
?>